The following libraries were used:
library(tidyverse)
library(here)
library(RColorBrewer)
library(plotly)
library(htmlwidgets)
Using nested for loops, R cycles through all six lakes and year by year. It then creates plots of all possible combinations.
load(file = here("data/data_tidy.RData")) # load object data_tidy
lakes <- unique(data_tidy$LakeName) # extract the names of the lakes
years <- unique(data_tidy$Year) # extract the years where counts were measured
plotlist <- list() # create an empty list to add to
for (i in 1:length(lakes)){
Ldata <- filter(data_tidy, LakeName == lakes[i]) # perform the analysis per lake
for (j in 1:length(years)){
Ydata <- filter(Ldata, Year == years[j]) # perform the analysis per year
if (nrow(Ydata) > 0) { # check if there is any data for this lake/year combination
data <- Ydata %>% mutate("Abundance_ind/L" = (Counts / Proportion_of_sample_counted) / Sampling_volume_net_L,
"Relative_abundance" = Counts / Total_individuals * 100) # calculate the abundances and relative abundances
summary <- data %>% group_by(Species, Month) %>% summarize(sum_counts = sum(Counts, na.rm = TRUE),
sum_total = sum(Total_individuals, na.rm = TRUE)) # calculate counts per month
summary <- summary %>% mutate("Relative_abundance" = sum_counts / sum_total * 100) # calculate relative abundance per month
getPalette <- colorRampPalette(brewer.pal(9, "Set1")) # prepare color palette containing the most distinct colors
result <- summary %>% ggplot(aes(x = Month, y = Relative_abundance, fill = Species))+
geom_col(color = "Black", linewidth = 0.05)+
labs(y = "Relative abundance (%)", title = paste0(lakes[[i]], " in ", years[[j]]))+
theme_minimal()+
scale_fill_manual(values = getPalette(24)) # generate plot of relative abundance per month
plotlist[[(length(plotlist)+1)]] <- result # add the plot to the list of plots
} else {
warning(paste0("No data for ", lakes[[i]], " in the year ", years[[j]])) # when there is no data for a lake/year combination, a warning is printed
}
}
}
## Warning: No data for Coneries Lake in the year 2010
## Warning: No data for Coneries Lake in the year 2011
## Warning: No data for Coneries Lake in the year 2012
## Warning: No data for Main Pond in the year 2010
## Warning: No data for Main Pond in the year 2011
## Warning: No data for Main Pond in the year 2012
## Warning: No data for Church Pond in the year 2010
## Warning: No data for Church Pond in the year 2011
## Warning: No data for Church Pond in the year 2012
## Warning: No data for Beeston Pond in the year 2010
## Warning: No data for Beeston Pond in the year 2011
## Warning: No data for Beeston Pond in the year 2012
The {plotly} package was used to create interactive plots, to make it possible to see the actual values of a bar as well as make it easier to see exactly what species it is since the colors are still relatively similar to one another.
htmltools::tagList(lapply(1:length(plotlist), function(x) { ggplotly(plotlist[[x]]) }))